package com.qeeyou.apps.android.craftsman.fragment;


import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.qeeyou.apps.android.craftsman.R;
import com.qeeyou.apps.android.craftsman.activity.publish.Publish2Activity;
import com.qeeyou.apps.android.craftsman.activity.publish.Publish3Activity;
import com.qeeyou.apps.android.craftsman.activity.publish.PublishActivity;
import com.qeeyou.apps.android.craftsman.po.InstallShopInfo;
import com.qeeyou.apps.android.craftsman.service.AppService;
import com.qeeyou.apps.android.craftsman.view.CustomLoadingDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xuchengxiong on 2017/12/14.
 */

public class PublishFragment extends BaseFragment {

    private LinearLayout btnGongZhuang;       //工装
    private LinearLayout btnDingZhi;          //全屋定制
    private LinearLayout btnChuGui;           //橱柜安装
    private LinearLayout btnDiaoDing;         //房屋吊顶
    private LinearLayout btnDiBan;            //安装地板
    private LinearLayout btnMenTao;           //门套安装
    private LinearLayout btnDanPeiMen;        //单配们
    private LinearLayout btnShouHou;          //售后

    private CustomLoadingDialog mLoadingDialog;
    private List<InstallShopInfo> shopList = new ArrayList<InstallShopInfo>();

    public PublishFragment() {
       super(R.layout.fragment_publish);
    }

    @Override
    protected void doOnCreate(Bundle savedInstanceState) {
        setView();
        setEvent();
        loadData();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    private void setView() {
        btnGongZhuang = (LinearLayout) this.findViewById(R.id.btnGongZhuang4PublishFragment);
        btnDingZhi = (LinearLayout) this.findViewById(R.id.btnDingZhi4PublishFragment);
        btnChuGui = (LinearLayout) this.findViewById(R.id.btnChuGui4PublishFragment);
        btnDiaoDing = (LinearLayout) this.findViewById(R.id.btnDiaoDing4PublishFragment);
        btnDiBan = (LinearLayout) this.findViewById(R.id.btnDiBan4PublishFragment);
        btnMenTao = (LinearLayout) this.findViewById(R.id.btnMenTao4PublishFragment);
        btnDanPeiMen = (LinearLayout) this.findViewById(R.id.btnDanPeiMen4PublishFragment);
        btnShouHou = (LinearLayout) this.findViewById(R.id.btnShouHou4PublishFragment);
    }

    private void setEvent() {
        btnGongZhuang.setOnClickListener(onItemClickListener);
        btnDingZhi.setOnClickListener(onItemClickListener);
        btnChuGui.setOnClickListener(onItemClickListener);
        btnDiaoDing.setOnClickListener(onItemClickListener);
        btnDiBan.setOnClickListener(onItemClickListener);
        btnMenTao.setOnClickListener(onItemClickListener);
        btnDanPeiMen.setOnClickListener(onItemClickListener);
        btnShouHou.setOnClickListener(onItemClickListener);
    }

    private void loadData() {
        if(shopList == null || shopList.size() == 0) {
            if(shopList == null) {
                shopList = new ArrayList<InstallShopInfo>();
            }
            String[] params = new String[] {};
            new GetInstallShopList().execute(params);
        } else {
            //设置数据
            btnGongZhuang.setTag(getShopInfo("工装"));
            btnDingZhi.setTag(getShopInfo("全屋定制"));
            btnChuGui.setTag(getShopInfo("橱柜安装"));
            btnDiaoDing.setTag(getShopInfo("房屋吊顶"));
            btnDiBan.setTag(getShopInfo("安装地板"));
            btnMenTao.setTag(getShopInfo("门套安装"));
            btnDanPeiMen.setTag(getShopInfo("单配门"));
            btnShouHou.setTag(getShopInfo("售后"));
        }
    }

    //Item项点击事件
    private View.OnClickListener onItemClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(view == btnGongZhuang) {
                InstallShopInfo shopInfo = (InstallShopInfo) btnGongZhuang.getTag();
                if(shopInfo == null) {
                    Toast.makeText(PublishFragment.this.getActivity(), "商品异常", Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent intent = new Intent(PublishFragment.this.getActivity(), PublishActivity.class);
                intent.putExtra("shopInfo", shopInfo);
                PublishFragment.this.getActivity().startActivity(intent);
            } else if(view == btnDingZhi || view == btnChuGui || view == btnDiaoDing ||
                    view == btnDiBan || view == btnMenTao || view == btnDanPeiMen) {
                InstallShopInfo shopInfo = (InstallShopInfo) view.getTag();
                if(shopInfo == null) {
                    Toast.makeText(PublishFragment.this.getActivity(), "商品异常", Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent intent = new Intent(PublishFragment.this.getActivity(), Publish2Activity.class);
                intent.putExtra("shopInfo", shopInfo);
                PublishFragment.this.getActivity().startActivity(intent);
            } else  {
                //TODO -- 售后
                InstallShopInfo shopInfo = (InstallShopInfo) btnShouHou.getTag();
                if(shopInfo == null) {
                    Toast.makeText(PublishFragment.this.getActivity(), "商品异常", Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent intent = new Intent(PublishFragment.this.getActivity(), Publish3Activity.class);
                intent.putExtra("shopInfo", shopInfo);
                PublishFragment.this.getActivity().startActivity(intent);
            }
        }
    };

    //获取安装商品列表
    class GetInstallShopList extends AsyncTask<String, Void, List<InstallShopInfo>> {
        public GetInstallShopList() {
            super();
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            if (mLoadingDialog == null) {
                mLoadingDialog = new CustomLoadingDialog(PublishFragment.this.getActivity());
            }
            mLoadingDialog.setMessage("正在获取安装商品列表...");
            mLoadingDialog.show();
        }

        @Override
        protected void onPostExecute(List<InstallShopInfo> shopList) {
            super.onPostExecute(shopList);
            if (mLoadingDialog != null) {
                mLoadingDialog.dismiss();
                mLoadingDialog = null;
            }
            if (shopList != null) {
                PublishFragment.this.shopList.clear();
                PublishFragment.this.shopList.addAll(shopList);
                //显示数据
                btnGongZhuang.setTag(getShopInfo("工装"));
                btnDingZhi.setTag(getShopInfo("全屋定制"));
                btnChuGui.setTag(getShopInfo("橱柜安装"));
                btnDiaoDing.setTag(getShopInfo("房屋吊顶"));
                btnDiBan.setTag(getShopInfo("安装地板"));
                btnMenTao.setTag(getShopInfo("门套安装"));
                btnDanPeiMen.setTag(getShopInfo("单配门"));
                btnShouHou.setTag(getShopInfo("售后"));
            }
        }

        @Override
        protected List<InstallShopInfo> doInBackground(String... params) {
            Map<String, String> paramMap = new HashMap<String, String>();
            List<InstallShopInfo> shopList = AppService.getInstance().getInstallShopList(paramMap);
            return shopList;
        }
    }

    private InstallShopInfo getShopInfo(String shopname) {
        if(shopname == null || shopname.trim().equalsIgnoreCase("")) {
            return null;
        }
        if(shopList == null || shopList.size() == 0) {
            return null;
        }
        int selectIndex = -1;
        for(int i = 0, n = shopList.size(); i < n; i++) {
            InstallShopInfo shopInfo = shopList.get(i);
            if(shopInfo.getShopname().trim().equalsIgnoreCase(shopname)) {
                selectIndex = i;
                break;
            }
        }
        if(selectIndex >= 0) {
            return shopList.get(selectIndex);
        } else {
            return null;
        }
    }

}
